package cn.rkylin.oms.aftersale.sysrefund.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.IDataBaseFactory;
import cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO;
import cn.rkylin.oms.aftersale.sysrefund.domain.SysRefund;
import cn.rkylin.oms.aftersale.sysrefund.vo.SysRefundVO;
import cn.rkylin.oms.aftersale.sysreturn.dao.ISysReturnDAO;
import cn.rkylin.oms.common.consts.YesNoConst;

/**
 * 类名:SysRefundDAOImpl <br/>
 * 作用: 系统退款单数据访问类. <br/>
 * 创建原因:	 系统退款单的数据访问. <br/>
 * 创建时间:     2017年10月19日 下午6:31:40 <br/>
 * @author   zhanghao
 * @version  v1.0 
 */
@Repository(value="sysRefundDAO")
public class SysRefundDAOImpl implements ISysRefundDAO {
    @Autowired
    IDataBaseFactory dao;
    
    /**
     * sysReturnDAO:退货单数据层对象.
     */
    @Autowired
    ISysReturnDAO sysReturnDAO;
    
    private String STATEMENT_PAGE_SELECT_BY_CONDITION = "pageSelectSysRefundByCondition";
    private String STATEMENT_PAGE_SELECT_BY_PRIMARYKEY = "selectByPrimaryKeySysRefund";
    private String STATEMENT_SELECT_DETAIL_BY_PRIMARYKEY = "selectDetailByPrimaryKeySysRefund";
    private String STATEMENT_SELECT_COUNT_BY_STATUS = "selectSysRefundCountByStatus";
    private String STATEMENT_INSERT_SYSREFUND = "insertSelectiveSysRefund";
    private String STATEMENT_UPDATE = "updateByPrimaryKeySelectiveSysRefund";
    private String STATEMENT_QUERY_RETURN_DIFF = "selectReturnDiffBySysRefundId";
    private String STATEMENT_QUERY_ONGOING_SYSREFUND = "selectOnGoingSysRefund";

    /**
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#deleteByPrimaryKey(java.lang.String)
     */
    @Override
    public int deleteByPrimaryKey(String refundId) {

        // TODO Auto-generated method stub
        return 0;
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#insert(cn.rkylin.oms.aftersale.sysrefund.domain.SysRefund)
     */
    @Override
    public int insert(SysRefund record) throws Exception {

        return dao.insert(STATEMENT_INSERT_SYSREFUND, record);
    }

    /**
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#insertSelective(cn.rkylin.oms.aftersale.sysrefund.domain.SysRefund)
     */
    @Override
    public int insertSelective(SysRefund record) {

        // TODO Auto-generated method stub
        return 0;
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#selectByPrimaryKey(java.lang.String)
     */
    @Override
    public SysRefund selectByPrimaryKey(String refundId) throws Exception {
        return dao.findOne(STATEMENT_PAGE_SELECT_BY_PRIMARYKEY, refundId);
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#updateByPrimaryKeySelective(cn.rkylin.oms.aftersale.sysrefund.domain.SysRefund)
     */
    @Override
    public int updateByPrimaryKeySelective(SysRefundVO record) throws Exception {
        return dao.update(STATEMENT_UPDATE, record);
    }

    /**
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#updateByPrimaryKey(cn.rkylin.oms.aftersale.sysrefund.domain.SysRefund)
     */
    @Override
    public int updateByPrimaryKey(SysRefund record) throws Exception{
        return dao.update(STATEMENT_UPDATE, record);
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#pageSelectByCondition(int, int, cn.rkylin.oms.aftersale.sysrefund.vo.SysRefundVO)
     */
    @Override
    public PageInfo<SysRefundVO> pageSelectByCondition(int page, int length, SysRefundVO queryParamVO) throws Exception {
        PageInfo<SysRefundVO> refundList = dao.find(page, length, STATEMENT_PAGE_SELECT_BY_CONDITION, queryParamVO);
        
        // 循环调用，为每条退货单取退货差异
        List<SysRefundVO> sysRefundVOList = refundList.getList();
        for (SysRefundVO sysRefundVO : sysRefundVOList) {
            String returnDiff = YesNoConst.NO;
            // 获取退款单对应的退货单数量
            Integer completeReturnCount = sysReturnDAO.selectReturnCountByRefundId(sysRefundVO.getRefundId());
            if(YesNoConst.YES.equalsIgnoreCase(sysRefundVO.getHasGoodReturn())) {
                // 对于有退货诉求的，判断是否有退货差异
                if(completeReturnCount==0) {
                    returnDiff = YesNoConst.YES;
                }else {
                    returnDiff = queryReturnDiffByRefundId(sysRefundVO.getRefundId());
                }
            }else {
                // 对于没有退货诉求的，判断退款单是否有已完成的退货单，如果有，则标识退货差异
                if(completeReturnCount>0) {
                    returnDiff = YesNoConst.YES;
                }
            }
            sysRefundVO.setReturnDiff(returnDiff);
        }
        return refundList;
    }

    /**
     * queryReturnDiffByRefundId:根据退款单ID查询是否退货异常. <br/>
     *
     * @author zhanghao
     * @param refundId
     * @return
     * @throws Exception 
     */
    @Override
    public String queryReturnDiffByRefundId(String refundId) throws Exception {
        // 如果退款单有对应的已完成的退货单，但计划退入商品上有差异标识，则有退货差异
        String returnString = YesNoConst.YES;
        String result = dao.findOne(STATEMENT_QUERY_RETURN_DIFF, refundId);
        if(StringUtils.isEmpty(result)||Integer.valueOf(result)==0) {
            returnString = YesNoConst.NO;
        }
        return returnString;
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#selectStateCount()
     */
    @Override
    public List<HashMap> selectStateCount(SysRefundVO queryParamVO) throws Exception {
        return dao.find(STATEMENT_SELECT_COUNT_BY_STATUS, queryParamVO);
    }

    @Override
    public List<SysRefund> findSysRefundList(SysRefund vo) throws Exception {
        return dao.findAllList("selectSysRefund", vo);
    }

    /**
     * @throws Exception 
     * @see cn.rkylin.oms.aftersale.sysrefund.dao.ISysRefundDAO#selectDetailByPrimaryKey(java.lang.String)
     */
    @Override
    public SysRefundVO selectDetailByPrimaryKey(String sysRefundId) throws Exception {
        SysRefundVO sysRefundVO = new SysRefundVO();
        sysRefundVO.setRefundId(sysRefundId);
        return dao.findOne(STATEMENT_SELECT_DETAIL_BY_PRIMARYKEY, sysRefundVO);
    }

    /**
     * 通过订单id获取退款单信息
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public List<SysRefund> selectSysRefundByOrderID(String orderId) throws Exception {
        return dao.findList("selectSysRefundByOrderID", orderId);
    }

    @Override
    public int selectCount(Map<String, Object> params) throws Exception {
        List resultList = dao.findList("selectRefundCountToday", params);
        if (resultList != null && resultList.size() > 0) {
            return (Integer)resultList.get(0);
        }
        
        return 0;
    }

    @Override
    public int getUnGoingCount(Map<String, String> paramMap) throws Exception {
        return dao.findOne(STATEMENT_QUERY_ONGOING_SYSREFUND, paramMap);
    }
}

